package com.gis.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

public class ResponseUtil {
	
	public static void writeJson(HttpServletResponse resp, RestResponse restResponse) throws IOException {
		resp.setContentType("text/json; charset=utf-8");
		resp.setHeader("Access-Control-Allow-Origin", "*");
		resp.setHeader("Access-Control-Allow-Method", "POST,GET");
		resp.setCharacterEncoding(Constant.UTF_8);
		PrintWriter writer;
		writer = resp.getWriter();
		writer.write(JSON.toJSONString(restResponse));
		writer.flush();
		writer.close();
	}
	
	public static <T> void writePage(HttpServletResponse resp, PageResult<T> pageResult) throws IOException {
		resp.setContentType("text/json; charset=utf-8");
		resp.setHeader("Access-Control-Allow-Origin", "*");
		resp.setHeader("Access-Control-Allow-Method", "POST,GET");
		resp.setCharacterEncoding(Constant.UTF_8);
		PrintWriter writer;
		writer = resp.getWriter();
		writer.write(JSON.toJSONString(pageResult));
		writer.flush();
		writer.close();
	}
	
	public static void writeImage(HttpServletResponse resp, byte[] bytes) throws IOException {
		resp.setContentType("image/jpeg");
		ServletOutputStream outputStream = resp.getOutputStream();
		outputStream.write(bytes);
	    outputStream.flush();
	    outputStream.close();
		
	}

}
